package com.open.capacity.salary.controller;

import com.open.capacity.commons.PageResult;
import com.open.capacity.commons.Result;
import com.open.capacity.salary.entity.GzItemEntry;
import com.open.capacity.salary.entity.GzItemPrint;
import com.open.capacity.salary.service.GzItemEntryService;
import com.open.capacity.salary.service.GzItemPrintService;
import com.open.capacity.vo.TreeVo;
import io.swagger.annotations.Api;
import org.apache.commons.collections.list.TreeList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @email
 * @date 2021-09-14 16:19:24
 */
@RestController
@RequestMapping("gzitementrys")
@Api(tags = "")
public class GzItemEntryController {

    @Autowired
    private GzItemEntryService gzItemEntryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @PreAuthorize("hasAnyAuthority('gz_item_print:gzitementry:list')")
    public PageResult list(@RequestParam Map<String, Object> params) {
        PageResult pageResult = gzItemEntryService.pageAll(params);
        return pageResult;
    }

    /**
     * 查询打印格式下拉
     *
     * @param companyId
     * @return
     */
    @GetMapping("/dropdownlist")
    public Result<List<TreeVo>> dropdownlist(@RequestParam(value = "companyId", required = false) String companyId) {
        Map<String, Object> params = new HashMap<>();
        params.put("companyId", companyId);
        List<GzItemEntry> zts = gzItemEntryService.findAll(params);
        List<TreeVo> treeList = new TreeList();
        zts.forEach(m -> {
            TreeVo node = new TreeVo();
            node.setId(m.getId());
            node.setText(m.getName());
            node.setValue(m.getId());
            node.setShowcheck(false);
            node.setCheckstate(0);
            node.setIsexpand(false);
            node.setComplete(true);
            node.setParentId("0");
            treeList.add(node);
        });

        return Result.succeed(treeList);
    }

    /**
     * 查询单条记录
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('gz_item_print:gzitementry:list')")
    public Result findById(String id) {
        return Result.succeed(gzItemEntryService.findById(id));
    }

    /**
     * 查询单条记录
     */
    @RequestMapping("/findByZtId")
    @PreAuthorize("hasAnyAuthority('gz_item_print:gzitementry:list')")
    public Result findByZtId(String ztId) {
        return Result.succeed(gzItemEntryService.findByZtId(ztId));
    }

    /**
     * 保存
     */
    @RequestMapping("/saveOrUpdate")
    @PreAuthorize("hasAnyAuthority('gz_item_print:gzitementry:save')")
    public Result save(@RequestBody @Valid GzItemEntry gzItemPrint, String keyValue) {
        if (StringUtils.isEmpty(keyValue)) {
            gzItemEntryService.save(gzItemPrint);
        } else {
            gzItemPrint.setId(keyValue);
            gzItemEntryService.update(gzItemPrint);
        }
        return Result.succeed("保存成功");
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAnyAuthority('gz_item_print:gzitementry:delete')")
    public Result delete(@PathVariable String id) {
        gzItemEntryService.delete(id);
        return Result.succeed("删除成功");
    }

}
